//
//  UserDefaultDemo.swift
//  firstProject
//
//  Created by 张彦辉 on 2017/5/4.
//  Copyright © 2017年 张彦辉. All rights reserved.
//

import UIKit

class UserDefaultDemo: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        let setBut = UIButton()
        setBut.backgroundColor = UIColor.blue
        setBut.setTitle("设置", for: .normal)
        setBut.addTarget(self, action: #selector(setButClick), for: .touchUpInside)
        
        let getBut = UIButton()
        getBut.backgroundColor = UIColor.darkGray
        getBut.setTitle("读取", for: .normal)
        getBut.addTarget(self, action: #selector(getButClick), for: .touchUpInside)
        
        self.view.addSubview(setBut)
        self.view.addSubview(getBut)
        
        setBut.snp.makeConstraints { (maker) in
            maker.top.equalTo(100)
            maker.left.equalTo(10)
        }
        getBut.snp.makeConstraints { (maker) in
            maker.top.equalTo(setBut)
            maker.left.equalTo(setBut.snp.right).offset(7)
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func setButClick() -> Void {
        let userDefault = UserDefaults.standard
        userDefault.set(arc4random() % 100, forKey: "intKey")
        userDefault.synchronize()
        PublicUtil.alert(obj: self, title: "提示", msg: "首选项保存成功")
    }
    func getButClick() -> Void {
        let userDefault = UserDefaults.standard
        let num = userDefault.integer(forKey: "intKey")
        PublicUtil.alert(obj: self, title: "提示", msg: "获取首选项的值\(num)")
    }
}
